package com.ms.oschina.ui.activity;

import java.util.ArrayList;
import java.util.Random;

import android.graphics.Color;
import android.graphics.Typeface;
import android.view.MotionEvent;
import android.view.View;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.ms.oschina.R;
import com.ms.oschina.base.BaseActivity;

/**
 * 技能饼的界面
 */
public class SkillsCakeActivity extends BaseActivity {
	private PieChart chart;

	private String[] skills = new String[] { "安卓", "JAVA", "IOS", "EE" };

	private void findViews() {
		chart = (PieChart) findViewById(R.id.pc_skillsradar);
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_skillscake;
	}

	@Override
	protected void initView() {
		findViews();
	}

	@Override
	protected void initData() {

		Typeface tf = Typeface.createFromAsset(getAssets(),
				"OpenSans-Regular.ttf");
		chart.setDescription("技能雷达");
		// 设置饼图的最内层的圆半径
		chart.setHoleRadius(47f);
		// 设置饼图的第二层的圆的半径
		chart.setTransparentCircleRadius(57f);
		chart.setCenterText("技能详情");
		chart.setCenterTextSize(18f);
		// 设置饼图的数据内容以百分比值的形式显示
		chart.setUsePercentValues(true);

		PieData chartData = generateDataPie();
		chartData.setValueFormatter(new PercentFormatter());
		chartData.setValueTextSize(11f);
		chartData.setValueTypeface(tf);
		chartData.setValueTextColor(Color.WHITE);
		// set data
		chart.setData(chartData);

		Legend l = chart.getLegend();
		l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
		// 指示器每部分Y轴方向的距离值
		l.setYEntrySpace(0f);
		// 指示器相对于原始位置的Y轴方向的一个偏移距离
		l.setYOffset(0f);

		// do not forget to refresh the chart
		// holder.chart.invalidate();
		chart.animateXY(900, 900);
	}

	/**
	 * generates a random ChartData object with just one DataSet
	 * 
	 * @return
	 */
	private PieData generateDataPie() {
		Random random = new Random();

		ArrayList<PieEntry> entries = new ArrayList<PieEntry>();

		for (int i = 0; i < 4; i++) {
			entries.add(new PieEntry(random.nextInt(500), skills[i]));//添加饼 ，和标题、安卓 、java
		}

		PieDataSet d = new PieDataSet(entries, "");
		// space between slices
		// 每部分之间的衔接距离值
		d.setSliceSpace(2f);
		d.setColors(ColorTemplate.COLORFUL_COLORS);

		PieData cd = new PieData(d);
		return cd;
	}

	protected void processClick(View v) {
		int id = v.getId();
		if (id == R.id.iv_clear_username) {
		} else if (id == R.id.iv_clear_password) {
		}
	}

	@Override
	protected CharSequence getBackActionBarTitle() {
		return "技能雷达";
	}

}
